package top.itmeans.yes.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import top.itmeans.yes.entity.HomeAdvertise;
import top.itmeans.yes.entity.Product;
import top.itmeans.yes.entity.ProductCategory;
import top.itmeans.yes.entity.Subject;
import top.itmeans.yes.mapper.HomeAdvertiseMapper;
import top.itmeans.yes.mapper.ProductCategoryMapper;
import top.itmeans.yes.mapper.ProductMapper;
import top.itmeans.yes.mapper.SubjectMapper;
import top.itmeans.yes.portal.domain.HomeContentResult;
import top.itmeans.yes.portal.mapper.HomeMapper;
import top.itmeans.yes.portal.service.HomeService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页内容管理Service实现类
 * @author Yiming.Cao
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private HomeAdvertiseMapper advertiseMapper;
    @Resource
    private HomeMapper homeMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList());
        //获取新品推荐
        result.setNewProductList(homeMapper.getNewProductList(0,4));
        //获取人气推荐
        result.setHotProductList(homeMapper.getHotProductList(0,4));
        //获取推荐专题
        result.setSubjectList(homeMapper.getRecommendSubjectList(0,4));
        return result;
    }

    @Override
    public Page<Product> recommendProductList(Integer pageSize, Integer pageNum) {
        Page<Product> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getPublishStatus,1);
        return productMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<ProductCategory> getProductCateList(Long parentId) {
        LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategory::getShowStatus,1);
        queryWrapper.eq(ProductCategory::getParentId,parentId);
        queryWrapper.orderByDesc(ProductCategory::getId);
        return productCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Subject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
        Page<Subject> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getShowStatus,1);
        if(cateId!=null){
            queryWrapper.eq(Subject::getCategoryId,cateId);
        }
        return subjectMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<Product> hotProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeMapper.getHotProductList(offset, pageSize);
    }

    @Override
    public List<Product> newProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeMapper.getNewProductList(offset, pageSize);
    }

    private List<HomeAdvertise> getHomeAdvertiseList() {
        LambdaQueryWrapper<HomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HomeAdvertise::getType,1).eq(HomeAdvertise::getStatus,1).orderByDesc(HomeAdvertise::getSort);
        return advertiseMapper.selectList(queryWrapper);
    }
}
